qui(
    function (require, exports, module) {
        live2VOD.Grid = function (options) {
            this.dao = live2VOD.Dao;
            this.config = live2VOD.Config;
            this.options = options || {};
        };
        Com.inherits(live2VOD.Grid, Ancestor.Grid).extend({
            runList: function (options, callback) {
                var self = this;
                self.$grid = $('#list');
                self.initSuggest();
                self.bindBasicEvent();
                self.initListGrid();
                self.config.PageBean = live2VOD.Util.createPageBean();
                self.query();
            },
            bindBasicEvent: function () {
                var self = this;
                $("button[name=createBtn]").bind("click", function (event) {
                    live2VOD.Util.jumpToEdit();
                })
                $("button[name=channelQuery]").bind("click", function (event) {
                    self.query();
                })
            },
            initListGrid: function (options, callback) {
                var self = this;
                var gridConfig = self.getGridConfig(self.config.TABLE_LIST);
                options = $.extend(true, {}, gridConfig, options);
                self.$grid.jqGrid(options);
            },
            query: function () {
                var self = this;
                self.dao.getList(self.getParam(), function (data) {
                    var retValue = null;
                    if (data.code === 200 && data.response != null && data.response.data != null) {
                        $.each(data.response.data, function (i, element) {
                            element.channeltypeShow = live2VOD.Util.formatter.channeltypeShow(null, null, element);
                            element.id = i;
                        })
                        self.config.PageBean.setNewData(data.response.data, self.config.TABLE_LIST.rowNum);
                    } else {
                        self.config.PageBean.setNewData([], self.config.TABLE_LIST.rowNum);
                    }
                    retValue = self.config.PageBean.getCur();
                    self.$grid[0].addJSONData(retValue)
                });
            },
            getParam: function () {
                var self = this;
                var params = {};
                var $form = $('form[name="basicFrm"]')
                var selectedElement = $("input[name=channeltype]").suggest("getSelectedItem")
                if (selectedElement == null) {
                    console.log("channeltype is empty")
                } else if (selectedElement.value != $("input[name=channeltype]").val()) {
                    console.log("value ne", selectedElement.value, $("input[name=channeltype]").val())
                } else {
                    params.channeltype = selectedElement.value;
                }
                var $elements = $.makeArray($form.prop('elements')).filter(function () {
                    return true; //this.name;
                });
                var filterNameArr = ["channeltype"];
                $.each($elements, function (i, element) {
                    if (element.type == "text") {
                        if (filterNameArr.indexOf(element.name) == -1) {
                            params[element.name] = $.trim($(element).val()) || '';
                        }
                    }
                });
                params.pageNo = 0;
                params.pageSize = 1000;
                return params;
            },
            initSuggest: function () {
                var self = this;
                var channelTypeSuggest = {
                    options: {
                        source: EpgWeb.urlConfig.redirect,
                        renderPanel: function (handler) {
                            self.dao.getChannelType(handler)
                        },
                        method: "post",
                        multiple: false,
                        parseValue: function () {
                            var val = this.element.val(),
                                ret = []
                            return ret;
                        },
                        getParam: function (term) {
                            var params = term.length == 0 ? {size: 100} : {word: term, size: 100}
                            var requestData = {url: live2VOD.Constant.thirdUrl_channel_type, type: "get", params: params, needCookie: true};
                            return {"request": JSON.stringify(requestData), timeSign: new Date().getTime()}
                        },

                        process: function (json) {
                            var ret = [],
                                self = this,
                                options = self.options,
                                itemData = null;
                            var data = null;
                            if (json == null) {
                                return [];
                            }
                            if (json.code === "A00000") {
                                data = $.isArray(json) ? json : json.data;
                            }
                            if (!data) {
                                return [];
                            }
                            $.map(data, function (item) {
                                itemData = {};
                                itemData[options.valueKey] = item["id"];
                                itemData[options.labelKey] = item["name"];
                                itemData["value"] = item["name"];
                                ret.push(itemData);
                            });
                            return ret;
                        },

                        template: function (item) {
                            return item["label"];
                        }
                    }
                }
                $("input[name=channeltype]").suggest(channelTypeSuggest.options);
                $("input[name=channeltype]").suggest("setSelectedItem", null);
            }
        })
    }
);